﻿Imports DevExpress.Utils
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class FormMesWorkReport
    Public usr As String
    Dim objGt As New clsGt
    Dim objGtNgDetails As New clsGtNgDetails
    Dim objGtPgdMesWorkReport As New clsGtPgdMesWorkReport
    Dim httpHelper As New HttpHelper
    Dim dt As New DataTable
    Private Sub FormMesWorkReport_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        setDgvProcedureData()
    End Sub

    Private Sub btnFresh_Click(sender As Object, e As EventArgs) Handles btnFresh.Click
        setDgvProcedureData()
    End Sub

    Private Sub setDgvProcedureData()
        Dim dtGt As DataTable = objGt.getListByWorkreportStatus("1")
        dtGt.Columns.Add("oper_type_name")
        If dtGt.Rows.Count > 0 Then
            For Each row As DataRow In dtGt.Rows
                If row("oper_type") = "0" Then
                    row("oper_type_name") = "骨架板"
                Else
                    row("oper_type_name") = "法兰板"
                End If
            Next
        End If
        dgvProcedure.AutoGenerateColumns = False
        dgvProcedure.DataSource = dtGt
    End Sub

    Private Sub btnOkDetails_Click(sender As Object, e As EventArgs) Handles btnOkDetails.Click
        If dgvProcedure.SelectedRows.Count > 0 Then
            Dim formMesWorkReportAdd As New FormMesWorkReportAdd()
            formMesWorkReportAdd.gt_sys_id = dgvProcedure.SelectedRows(0).Cells("Col_sys_id").Value.ToString()
            formMesWorkReportAdd.ShowDialog(Me)
        End If
    End Sub

    Private Sub dgvProcedure_SelectionChanged(sender As Object, e As EventArgs) Handles dgvProcedure.SelectionChanged
        If dgvProcedure.SelectedRows.Count > 0 Then
            setDgvData("1")
        End If
    End Sub

    Private Sub setDgvData(oper_type As String)
        dt = objGtNgDetails.getListByGtSysId(dgvProcedure.SelectedRows(0).Cells("Col_sys_id").Value.ToString())
        dt.Columns.Add("procedure_no_name")
        dt.Columns.Add("validity_name")
        If dt.Rows.Count > 0 Then
            For Each row As DataRow In dt.Rows
                If oper_type.Equals("0") Then
                    If row("procedure_no") = "00" Then
                        row("procedure_no_name") = "领料"
                    End If
                    If row("procedure_no") = "0" Then
                        row("procedure_no_name") = "校平"
                    End If
                    If row("procedure_no") = "1" Then
                        row("procedure_no_name") = "喷砂清洗"
                    End If
                    If row("procedure_no") = "2" Then
                        row("procedure_no_name") = "六工位"
                    End If
                End If
                If oper_type.Equals("1") Then
                    If row("procedure_no") = "00" Then
                        row("procedure_no_name") = "领料"
                    End If
                    If row("procedure_no") = "1" Then
                        row("procedure_no_name") = "喷砂清洗"
                    End If
                    If row("procedure_no") = "2" Then
                        row("procedure_no_name") = "三工位"
                    End If
                End If
                If row("validity") = "0" Then
                    row("validity_name") = "无效"
                Else
                    row("validity_name") = "有效"
                End If
            Next
        End If
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt
    End Sub

    Private Sub btnMesWorkReport_Click(sender As Object, e As EventArgs) Handles btnMesWorkReport.Click
        'Dim mes_bg_rtn As Integer = mesbg("11", "11", "1", "03.05.03.331", "测试425-1")
        If dgvProcedure.SelectedRows.Count > 0 Then
            Dim b_gtSysId As String = dgvProcedure.SelectedRows(0).Cells("Col_sys_id").Value.ToString()
            Dim b_gtPgdMesSysId As String = dgvProcedure.SelectedRows(0).Cells("b_gt_pgd_mes_sys_id").Value.ToString()
            Dim b_gtPgdMesProjectCode As String = dgvProcedure.SelectedRows(0).Cells("mes_project_code").Value.ToString()
            Dim oper_num As String = dgvProcedure.SelectedRows(0).Cells("Col_ok_num").Value.ToString()
            Dim oper_num_ng As String = dgvProcedure.SelectedRows(0).Cells("Col_ng_num").Value.ToString()
            Dim b_gtPgdMes_gx As String = dgvProcedure.SelectedRows(0).Cells("material_code_pgd").Value.ToString()
            btnMesWorkReport.Enabled = False
            Dim dtGtPgdMesWorkReportOk As DataTable = objGtPgdMesWorkReport.getListByGtPgdmesSysId("ok", b_gtSysId)
            If dtGtPgdMesWorkReportOk.Rows.Count > 0 Then
                If MsgBox("此生产记录已报工过，确认要进行再次合格报工吗？", vbYesNo, "提示") = vbYes Then
                    objGtPgdMesWorkReport.insert("ok", b_gtSysId, b_gtPgdMesSysId, oper_num, usr)
                    Dim mes_bg_rtn00 As String = mesbg(b_gtSysId, b_gtPgdMesSysId, oper_num, b_gtPgdMes_gx, b_gtPgdMesProjectCode)
                    Dim jo00 As JObject = JsonConvert.DeserializeObject(mes_bg_rtn00)
                    Dim code00 As String = jo00("code").ToString() '输出0
                    Dim msg00 As String = jo00("msg").ToString() '输出操作成功
                    If code00.Equals("0") Then
                        objGt.updatePgdMesWorkreportStatus(b_gtSysId, "1")
                        MsgBox("报工成功", vbYes, "提示")
                    Else
                        objGt.updatePgdMesWorkreportStatus(b_gtSysId, "-1")
                        MsgBox("报工失败：" & msg00, vbYes, "提示")
                    End If
                End If
            Else
                If MsgBox("确认要进行合格报工吗？", vbYesNo, "提示") = vbYes Then
                    objGtPgdMesWorkReport.insert("ok", b_gtSysId, b_gtPgdMesSysId, oper_num, usr)
                    Dim mes_bg_rtn As String = mesbg(b_gtSysId, b_gtPgdMesSysId, oper_num, b_gtPgdMes_gx, b_gtPgdMesProjectCode)
                    Dim jo As JObject = JsonConvert.DeserializeObject(mes_bg_rtn)
                    Dim code As String = jo("code").ToString() '输出0
                    Dim msg As String = jo("msg").ToString() '输出操作成功
                    If code.Equals("0") Then
                        objGt.updatePgdMesWorkreportStatus(b_gtSysId, "1")
                        MsgBox("报工成功", vbYes, "提示")
                    Else
                        objGt.updatePgdMesWorkreportStatus(b_gtSysId, "-1")
                        MsgBox("报工失败：" & msg, vbYes, "提示")
                    End If
                End If
            End If
            Dim dtGtPgdMesWorkReportNg As DataTable = objGtPgdMesWorkReport.getListByGtPgdmesSysId("ng", b_gtSysId)
            If dtGtPgdMesWorkReportNg.Rows.Count > 0 Then
                If oper_num_ng.Equals("0") = False Then
                    If MsgBox("此生产记录已报工过，确认要进行再次不合格报工吗？", vbYesNo, "提示") = vbYes Then
                        objGtPgdMesWorkReport.insert("ng", b_gtSysId, b_gtPgdMesSysId, oper_num, usr)
                        Dim rtn_mes_bg11 As String = mesbgNg(b_gtSysId, b_gtPgdMesSysId, oper_num_ng, b_gtPgdMes_gx, b_gtPgdMesProjectCode)
                        If String.IsNullOrEmpty(rtn_mes_bg11) = False Then
                            Dim jo11 As JObject = JsonConvert.DeserializeObject(rtn_mes_bg11)
                            Dim code11 As String = jo11("code").ToString() '输出0
                            Dim msg11 As String = jo11("msg").ToString() '输出操作成功
                            If code11.Equals("0") Then
                                objGt.updatePgdMesWorkreportStatus(b_gtSysId, "1")
                                MsgBox("报工成功", vbYes, "提示")
                            Else
                                objGt.updatePgdMesWorkreportStatus(b_gtSysId, "-1")
                                MsgBox("报工失败：" & msg11, vbYes, "提示")
                            End If
                        Else
                            MsgBox("未获取到报工返回信息", vbYes, "提示")
                        End If
                    End If
                Else
                    'MsgBox("不合格数量为0，无法进行不合格报工", vbYes, "提示")
                End If
            Else
                If oper_num_ng.Equals("0") = False Then
                    If MsgBox("确认要进行不合格报工吗？", vbYesNo, "提示") = vbYes Then
                        objGtPgdMesWorkReport.insert("ng", b_gtSysId, b_gtPgdMesSysId, oper_num, usr)
                        Dim rtn_mes_bg1X As String = mesbgNg(b_gtSysId, b_gtPgdMesSysId, oper_num_ng, b_gtPgdMes_gx, b_gtPgdMesProjectCode)
                        If String.IsNullOrEmpty(rtn_mes_bg1X) = False Then
                            Dim jo1X As JObject = JsonConvert.DeserializeObject(rtn_mes_bg1X)
                            Dim code1X As String = jo1X("code").ToString() '输出0
                            Dim msg1X As String = jo1X("msg").ToString() '输出操作成功
                            If code1X.Equals("0") Then
                                objGt.updatePgdMesWorkreportStatus(b_gtSysId, "1")
                                MsgBox("报工成功", vbYes, "提示")
                            Else
                                objGt.updatePgdMesWorkreportStatus(b_gtSysId, "-1")
                                MsgBox("报工失败：" & msg1X, vbYes, "提示")
                            End If
                        Else
                            MsgBox("未获取到报工返回信息", vbYes, "提示")
                        End If
                    End If
                Else
                    'MsgBox("不合格数量为0，无法进行不合格报工", vbYes, "提示")
                End If
            End If
            btnMesWorkReport.Enabled = True
        End If
    End Sub

    Private Function mesbg(gtSysId As String, gtPgdMesSysId As String, oper_num As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """qualifiednumber"":""" & Double.Parse(oper_num) & """," '报工的合格数
        params &= """unqualifiednumber"":""" & 0.00 & """," '报工的不合格数
        params &= """productorname"":""" & productorname & """," '报工产品编码
        params &= """procedurename"":""" & "喷涂" & """," '报工工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """unqualifiedReason"":""" & "" & """" '不合格原因
        params &= "}"
        Return httpHelper.HttpPostWebService("dispatchingOrderBG", params)
    End Function

    Private Function mesbgNg(gtSysId As String, gtPgdMesSysId As String, oper_num As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim rtn As String = String.Empty
        For index = 0 To dt.Rows.Count - 1
            Dim params As String = "{"
            params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
            params &= """qualifiednumber"":""" & 0.00 & """," '报工的合格数
            params &= """unqualifiednumber"":""" & Double.Parse(dt.Rows(index)("ng_num").ToString()) & """," '报工的不合格数
            params &= """productorname"":""" & productorname & """," '报工产品编码
            params &= """procedurename"":""" & "喷涂" & """," '报工工序
            params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
            params &= """unqualifiedReason"":""" & dt.Rows(index)("ng_reason") & """" '不合格原因
            params &= "}"
            rtn = httpHelper.HttpPostWebService("dispatchingOrderBG", params)
        Next
        Return rtn
    End Function
End Class


Public Class entityPgdMesGt
    Public pgd_code As String
    Public oper_date As String
    Public mes_project_code As String
    Public mes_project_name As String
    Public support_model As String
    Public count As String
    Public pgd_code0 As String
    Public oper_type0 As String
    Public type_value0 As String
    Public oper_count0 As String
    Public pgd_code1 As String
    Public oper_type1 As String
    Public type_value1 As String
    Public oper_count1 As String
    Public material_code As String
    Public note As String
    Public validity As String
    Public status As String
    Public origin As String
    Public Ope As String
    Public start_time As String
    Public end_time As String
End Class